package com.wilcom.system.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wilcom.system.domain.Maintenance;
import com.wilcom.system.domain.Repairrequest;
import com.wilcom.system.domain.Repairtask;
import com.wilcom.system.domain.User;
import com.wilcom.system.mapper.RepairrequestMapper;
import com.wilcom.system.mapper.RepairtaskMapper;
import com.wilcom.system.mapper.UserMapper;
import com.wilcom.system.model.request.repairtask.AcceptRepairTaskRequest;
import com.wilcom.system.model.request.repairtask.CreateRepairTasks;
import com.wilcom.system.model.request.repairtask.GetAssignedTasksRequest;
import com.wilcom.system.model.request.repairtask.GetRepairTasksBy;
import com.wilcom.system.model.respone.RepairTaskDetail;
import com.wilcom.system.service.RepairtaskService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lishi
 * @description 针对表【repairtask】的数据库操作Service实现
 * @createDate 2024-06-22 02:34:05
 */
@Service
public class RepairtaskServiceImpl extends ServiceImpl<RepairtaskMapper, Repairtask>
        implements RepairtaskService {
    @Resource
    RepairrequestMapper repairrequestMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public QueryWrapper<Repairrequest> getQueryWrapper(AcceptRepairTaskRequest postQueryRequest) {
        QueryWrapper<Repairtask> queryWrapperRepairtask = new QueryWrapper<>();

        queryWrapperRepairtask.eq("assigned_technician_id", postQueryRequest.getUserId());
        queryWrapperRepairtask.eq("status", "assigned");

        List<Repairtask> repairtasks = this.list(queryWrapperRepairtask);

        List<Integer> requestIds = repairtasks.stream()
                .map(Repairtask::getRequestId)
                .collect(Collectors.toList());
        QueryWrapper<Repairrequest> queryWrapperRepairrequest = new QueryWrapper<>();
        queryWrapperRepairrequest.in("request_id", requestIds);
//
//        user . order_count +1
        return queryWrapperRepairrequest;
    }

    @Override
    public QueryWrapper<Repairtask> getQueryWrapperTask(GetAssignedTasksRequest postQueryRequest) {
        QueryWrapper<Repairtask> queryWrapperRepairtask = new QueryWrapper<>();
        Integer userId = postQueryRequest.getUserId();
        queryWrapperRepairtask.eq("assigned_technician_id", userId);
        queryWrapperRepairtask.eq("status", "assigned");

        return null;
    }

    @Override
    public List<RepairTaskDetail> getAssignedTasksDetailByUserId(GetAssignedTasksRequest postQueryRequest) {
        QueryWrapper<Repairtask> queryWrapperRepairtask = new QueryWrapper<>();
        Integer userId = postQueryRequest.getUserId();
        queryWrapperRepairtask.eq("assigned_technician_id", userId);

        List<Repairtask> repairtasks = this.list(queryWrapperRepairtask);
        List<RepairTaskDetail> repairTaskDetails = repairtasks.stream()
                .map(repairtask -> {
                    RepairTaskDetail repairTaskDetail = new RepairTaskDetail();
                    repairTaskDetail.setTaskId(repairtask.getTaskId());
                    repairTaskDetail.setRequestId(repairtask.getRequestId());
                    repairTaskDetail.setAssignedTechnicianId(repairtask.getAssignedTechnicianId());
                    repairTaskDetail.setStatus(repairtask.getStatus());
                    return repairTaskDetail;
                }).collect(Collectors.toList());
        QueryWrapper<Repairrequest> queryWrapperRepairrequest = new QueryWrapper<>();
        queryWrapperRepairrequest.in("request_id", repairTaskDetails.stream().map(RepairTaskDetail::getRequestId).collect(Collectors.toList()));
//        repairrequestMapper
        List<Repairrequest> repairrequests = repairrequestMapper.selectList(queryWrapperRepairrequest);
//        合并
        repairTaskDetails.forEach(repairTaskDetail -> {
            repairrequests.forEach(repairrequest -> {
                if (repairrequest.getRequestId().equals(repairTaskDetail.getRequestId())) {
                    repairTaskDetail.setDescription(repairrequest.getDescription());
                    repairTaskDetail.setLocation(repairrequest.getLocation());
                    repairTaskDetail.setTime(repairrequest.getTime());
                    repairTaskDetail.setUrl(repairrequest.getUrl());
                    repairTaskDetail.setUrgencyLevel(repairrequest.getUrgencyLevel());
                    repairTaskDetail.setCreatedAt(repairrequest.getCreatedAt());
                    repairTaskDetail.setUpdatedAt(repairrequest.getUpdatedAt());
                }
            });
        });

        return repairTaskDetails;


    }


    @Override
    public Boolean createRepairTasks(CreateRepairTasks createRequest) {
        if (createRequest.getRequestId() == null)
            return false;
        Repairtask createRepairtask = new Repairtask();
//
        QueryWrapper<User> queryWrapperUser = new QueryWrapper<>();
        queryWrapperUser.eq("role", "technician");
//        选择order_count 最少的
        queryWrapperUser.orderByAsc("order_count");
        queryWrapperUser.last("limit 1");
        User user = userMapper.selectOne(queryWrapperUser);
        createRepairtask.setAssignedTechnicianId(user.getUserId());

        createRepairtask.setRequestId(createRequest.getRequestId());
        createRepairtask.setStatus("assigned");
        createRepairtask.setCreatedAt(new Date());
        createRepairtask.setUpdatedAt(new Date());
        try {
            return this.save(createRepairtask);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<RepairTaskDetail> getUserRepairTaskBy(GetRepairTasksBy getRepairTasksBy) {
        System.out.println(getRepairTasksBy.toString());
        QueryWrapper<Repairtask> queryWrapperRepairtask = new QueryWrapper<>();
        if (getRepairTasksBy.getUserId() == null || getRepairTasksBy.getUserId() <= 0) {
            return null;
        }
        queryWrapperRepairtask.eq("assigned_technician_id", getRepairTasksBy.getUserId());
        if (StringUtils.isNotEmpty(getRepairTasksBy.getStatus())) {
            queryWrapperRepairtask.eq("status", getRepairTasksBy.getStatus());
        }
//        若根据assigned_technician_id条件查询 为null


        List<Repairtask> repairtasks = this.list(queryWrapperRepairtask);
        if (repairtasks.size() == 0) {
            return new ArrayList<>();
        }
        List<RepairTaskDetail> repairTaskDetails = repairtasks.stream()
                .map(repairtask -> {
                    RepairTaskDetail repairTaskDetail = new RepairTaskDetail();
                    repairTaskDetail.setTaskId(repairtask.getTaskId());
                    repairTaskDetail.setRequestId(repairtask.getRequestId());
                    repairTaskDetail.setAssignedTechnicianId(repairtask.getAssignedTechnicianId());
                    repairTaskDetail.setStatus(repairtask.getStatus());
                    return repairTaskDetail;
                }).collect(Collectors.toList());
        QueryWrapper<Repairrequest> queryWrapperRepairrequest = new QueryWrapper<>();
        queryWrapperRepairrequest.in("request_id", repairTaskDetails.stream().map(RepairTaskDetail::getRequestId).collect(Collectors.toList()));
//        repairrequestMapper
        List<Repairrequest> repairrequests = repairrequestMapper.selectList(queryWrapperRepairrequest);
//        合并
        repairTaskDetails.forEach(repairTaskDetail -> {
            repairrequests.forEach(repairrequest -> {
                if (repairrequest.getRequestId().equals(repairTaskDetail.getRequestId())) {
                    repairTaskDetail.setDescription(repairrequest.getDescription());
                    repairTaskDetail.setLocation(repairrequest.getLocation());
                    repairTaskDetail.setTime(repairrequest.getTime());
                    repairTaskDetail.setUrl(repairrequest.getUrl());
                    repairTaskDetail.setUrgencyLevel(repairrequest.getUrgencyLevel());
                    repairTaskDetail.setCreatedAt(repairrequest.getCreatedAt());
                    repairTaskDetail.setUpdatedAt(repairrequest.getUpdatedAt());
                }
            });
        });
        if (StringUtils.isNotEmpty(getRepairTasksBy.getUrgencyLevel())) {
//            选择紧急程度 = getRepairTasksBy.getUrgencyLevel()
            List<RepairTaskDetail> repairTaskDetails1 = repairTaskDetails.stream().filter(repairTaskDetail -> {
                if (repairTaskDetail.getUrgencyLevel().equals(getRepairTasksBy.getUrgencyLevel())) {
                    return true;
                } else {
                    return false;
                }
            }).collect(Collectors.toList());
            return repairTaskDetails1;
        }

        return repairTaskDetails;

    }
}




